package com.qyapp.sdkplatform.sdk;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.tencent.connect.UserInfo;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONObject;

/**
 * Created by 2ustc on 2017/6/26.
 */

public class QQPlatform extends Platform {
    static QQPlatform platform;
    String mAppid;
    Tencent mTencent;
    IUiListener mLoginListener;

    public static QQPlatform share() {
        return platform == null ? (platform = new QQPlatform()) : platform;
    }


    @Override
    public void init(Activity activity, IPlatformCallback callback, String... args) {
        super.init(activity, callback, args);
        this.mAppid = args[0];
        this.mTencent = Tencent.createInstance(this.mAppid, this.mActivity.getApplicationContext());
    }


    @Override
    public void login() {
        if (mTencent != null) {
            this.mLoginListener = new PlatformUiListener(mPlatformCallback) {
                @Override
                public void onComplete(Object o) {
                    try {
                        JSONObject json = (JSONObject) o;
                        String token = json.getString(Constants.PARAM_ACCESS_TOKEN);
                        String expires = json.getString(Constants.PARAM_EXPIRES_IN);
                        mOpenId = json.getString(Constants.PARAM_OPEN_ID);
                        if (!TextUtils.isEmpty(token) && !TextUtils.isEmpty(expires) && !TextUtils.isEmpty(mOpenId)) {
                            mTencent.setAccessToken(token, expires);
                            mTencent.setOpenId(mOpenId);
                        }
                    } catch (Exception e) {
                    }
                    if (mTencent.isSessionValid()) {
                        UserInfo userInfo = new UserInfo(mActivity.getApplication(), mTencent.getQQToken());
                        userInfo.getUserInfo(new PlatformUiListener(platformCallback) {
                            @Override
                            public void onComplete(Object o) {
                                JSONObject json = (JSONObject) o;
                                if (mPlatformCallback != null) {
                                    mPlatformCallback.onAuthSuccess(json,QQPlatform.this);
                                }
                            }
                        });
                    }
                }
            };
            mTencent.login(this.mActivity, "all", this.mLoginListener);
        }
    }


    @Override
    public void destroy() {
        mLoginListener = null;
        platform = null;
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        this.mTencent.onActivityResultData(requestCode, resultCode, data, this.mLoginListener);
    }


    public abstract class PlatformUiListener implements IUiListener {
        IPlatformCallback platformCallback;

        public PlatformUiListener(IPlatformCallback platformCallback) {
            this.platformCallback = platformCallback;
        }

        @Override
        public abstract void onComplete(Object o);

        @Override
        public void onError(UiError uiError) {
            this.platformCallback.onAuthFaild(uiError.errorCode, uiError.errorMessage);
        }

        @Override
        public void onCancel() {
            this.platformCallback.onAuthCancel();
        }
    }

}
